package receiver;
import utils.Selection;
/**
 * @(#) Buffer.java
 */

public class Buffer {
	private PressePapier pp;
	private int curseur;

	private StringBuffer zoneTxt;
	private Selection selec;
	public Integer debSelect;

	public Integer longSelect;

	public Buffer() {
		pp = new PressePapier();
		this.curseur = 0;
		zoneTxt = new StringBuffer("");
	}
	
	public void setSelection(Selection selec){
		this.selec=selec;
	}

	public int getCurseur() {
		return curseur;
	}

	public void setCurseur(int curseur) {
		if (curseur>=0) 
			this.curseur = curseur;
		else 
			this.curseur=0;
	}
	
	public void copier() {
		if(selec.getLongSelection()>0) {
			pp.setZoneTxt(this.zoneTxt.substring(selec.getDebSelection(), selec.getLongSelection()+selec.getDebSelection()));
		}
	}
	
	public void coller() {
		inserer(pp.getZoneTxt().toString());
	}

	public void couper() {
		copier();
		int deb = selec.getDebSelection();
		int end = selec.getDebSelection()+selec.getLongSelection();
		this.zoneTxt.delete(deb, end);
		this.curseur = this.curseur - (end - deb);
	}
	
	public void effacer(){

		this.curseur--;
		this.zoneTxt.deleteCharAt(this.curseur);
	}
	
	public StringBuffer getZoneTxt() {
		return zoneTxt;
	}

	public void inserer(String s) {
		StringBuffer stb = new StringBuffer(s);
		if(this.zoneTxt.length()>0){
			zoneTxt.insert(curseur, stb);
		}
		else{
			zoneTxt.append(stb);
		}
		this.curseur+=stb.length();
	}

	public PressePapier getPp() {
		return pp;
	}

	public void setPp(PressePapier pp) {
		this.pp = pp;
	}
}
